<template>
  <div>
    <el-card class="box-card">
      <div>
        <span class="el-icon-goods"></span>
        <span> 商品类型数据列表</span>
        <el-button
          style="float: right"
          type="button"
          size="small"
          @click="goAddPage"
          >添加</el-button
        >
      </div>
    </el-card>

    <el-table :data="productAttrList" border style="width: 100%">
      <el-table-column align="center" prop="id" label="编号"></el-table-column>
      <el-table-column align="center" prop="name" label="分类名称" style=" text-align: center;"> </el-table-column>
      <el-table-column align="center" prop="param_count" label="属性数量"> </el-table-column>
      <el-table-column align="center" prop="show_type" label="参数数量"> </el-table-column>
      <el-table-column align="center" prop="operation" label="操作">
          <template slot-scope="scope">
            <el-button @click="editFun(scope.row.id)" size="small" style="background: #409eff; color: #fff; width:56px;height: 28px;">编辑</el-button>
            <el-button @click="delFun(scope.row.id)" type="danger" size="small" style="background: #f56c6c; color: #fff; width:56px;height: 28px;">删除</el-button>
          </template>
      </el-table-column>
    </el-table>


    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="listQuery.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
//引入
import { productAttrList,DeleproductAttr,productAttrAdd } from "@/api/product";
export default {
  name: "productAttr",
  data: function () {
    return {
      productAttrList: [],
      listQuery:{
       pageSize: 5, //一页10条数据
        pageNum: 1, //当前页
        name: "", //商品名称
        id: null, //货号
        param_count: null, //上架状态
        show_type:null, //审核状态
      },
      total:this.total,//总数量
    };
  },
  //验证规则
  rules: {
    name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
          {
            min: 3,
            max: 100,
            message: "长度在3-100个字符之间",
            trigger: "blur",
          },
        ],
  },
   watch: {
    //监听级联菜单的选中的数据是否发生变化了
    productCateValue(newV) {
      console.log(newV);
      //找到 数组的1号元素，赋值给 product_category_id
      this.listQuery.product_category_id = newV[1];
    },
  },
  methods: {
    async getproductAttrList() {
      const res = await productAttrList(this.listQuery);
      console.log(res);
      this.productAttrList = res.data;
      this.total = res.total
    },
    //跳转添加页面
    goAddPage(){
       this.$prompt('类型名称', '添加类型', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: '',
        }).then(({ value }) => {
          let obj = {
            name:value
          }
          productAttrAdd(obj).then((res) => {
            // console.log(res);
            if (res.status == 200) {
              //提示
              this.getproductAttrList();
              this.$message({
                message: "恭喜你，添加了" + value,
                type: "success",
              });
            }
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });       
        });
    },
     //修改
    editFun(id) {
      console.log("修改触发了", id);
    },
    //删除
   delFun(id) {
      console.log("删除触发了", id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用删除接口
          DeleproductAttr({ id: id }).then((res) => {
            console.log(res);
            if (res.type == "success") {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getproductAttrList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //5.修改每页显示的条数
    handleSizeChange(val) {
      console.log("修改每页显示的条数 触发了", val);
      this.listQuery.pageSize = val;
      this.listQuery.pageNum = 1;
      //重新获取数据
      this.getproductAttrList();
    },
    //6.修改当前页的页码
    handleCurrentChange(val) {
      console.log("修改当前页的页码 触发了", val);
      this.listQuery.pageNum = val;
      this.getproductAttrList();
    },
  },
  mounted() {
    this.getproductAttrList();
  },
};
</script>

<style>

</style>